package com.tencent.iov.cowin.befsale.clue.service.cus.impl;

import com.github.pagehelper.Page;
import com.tencent.iov.cowin.befsale.clue.controller.cus.assembler.DealerCustomerFollowPlanAssembler;
import com.tencent.iov.cowin.befsale.clue.controller.cus.assembler.OemCustomerFollowPlanAssembler;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.ClueIntentionGradeDataRequest;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.QueryCustomerFollowPlanRequest;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.response.CustomerFollowPlanResponse;
import com.tencent.iov.cowin.befsale.clue.domain.cus.po.OemCustomerFollowPlan;
import com.tencent.iov.cowin.befsale.clue.repository.cus.OemCustomerFollowPlanManager;
import com.tencent.iov.cowin.befsale.clue.service.cus.OemCustomerFollowPlanService;
import com.software.boot.common.exception.BusinessException;
import com.software.boot.common.exception.NEError;
import com.software.boot.common.model.dao.PageRequest;
import com.software.boot.common.model.dao.PageResponse;
import com.software.boot.common.util.BeanUtil;
import com.software.boot.common.util.EmptyUtils;
import com.software.boot.common.util.ValidateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Describe: 总部计划跟进线索表_Service实现层
 * @author: xiamu
 * @created: 2021-12-21 18:37:29
 * @version: 1.0
 */
@Service
public class OemCustomerFollowPlanServiceImpl implements OemCustomerFollowPlanService {

    @Autowired
    private OemCustomerFollowPlanManager oemCustomerFollowPlanManager;

    /**
     * 通过ID获取详细信息
     *
     * @param: request
     * @return: OemCustomerFollowPlan对象
     */
    @Override
    public CustomerFollowPlanResponse getDetailById(Long id) {
        /** 1 验证请求参数 */
        ValidateUtils.checkObjNull(id, "请求参数不能为空");
        ValidateUtils.checkIntNotNullAndGTZero(id.intValue(), "ID不能为空或小于0");
        /** 2 处理业务 */
        OemCustomerFollowPlan oemCustomerFollowPlan = oemCustomerFollowPlanManager.getDetailById(id);
        if (EmptyUtils.isNotEmpty(oemCustomerFollowPlan)) {
            return BeanUtil.copy(oemCustomerFollowPlan, CustomerFollowPlanResponse.class);
        }
        /** 3 返回结果 */
        return null;
    }

    /**
     * 单条数据插入OemCustomerFollowPlan
     *
     * @param request
     * @return 插入的条数
     */
    @Override
    public Long insert(OemCustomerFollowPlan request) {
        /** 1 验证请求参数 */
        /** 2 处理业务 */
        request = OemCustomerFollowPlanAssembler.initOemCustomerFollowPlan(request);
        int count = oemCustomerFollowPlanManager.insert(request);
        // 封装返回值
        if (count <= 0) {
            throw new BusinessException(NEError.BUSINESS_ERROR, "保存失败");
        }
        return request.getId();
    }

    /**
     * 批量插入OemCustomerFollowPlan
     *
     * @param requests
     * @return 批量插入的条数
     */
    @Override
    public int insertBatch(List<OemCustomerFollowPlan> requests) {
        /** 1 验证请求参数 */
        /** 2 处理业务 */
        int count = oemCustomerFollowPlanManager.insertBatch(requests);
        /** 3 封装返回值 */
        return count;
    }


    /**
     * 单条OemCustomerFollowPlan更新
     *
     * @param request
     * @return 更新的条数
     */
    @Override
    public int update(OemCustomerFollowPlan request) {

        /** 1 验证请求参数 */
        /** 2 处理业务 */
        request = OemCustomerFollowPlanAssembler.initUpdateOemCustomerFollowPlan(request);
        int count = oemCustomerFollowPlanManager.update(request);
        /** 3 返回对象 */
        return count;
    }

    /**
     * 通过ID删除数据
     *
     * @param request
     * @return 删除的条数
     */
    @Override
    public int delete(OemCustomerFollowPlan request) {
        Long id = request.getId();
        /** 1 验证请求参数 */
        ValidateUtils.checkObjNull(id, "请求参数不能为空");
        ValidateUtils.checkIntNotNullAndGTZero(id.intValue(), "ID不能为空或小于0");
        /** 2 处理业务 */
        OemCustomerFollowPlan oemCustomerFollowPlan = new OemCustomerFollowPlan();
        oemCustomerFollowPlan.setId(id);
//		oemCustomerFollowPlan.setDelete(DeleteEnum.DETELED.getCode());
        int count = oemCustomerFollowPlanManager.update(oemCustomerFollowPlan);
        /** 3 返回对象 */
        return count;
    }

    /**
     * 根据条件查询对象对象list列表
     *
     * @param request
     * @return 查询结果
     */
    @Override
    public List<OemCustomerFollowPlan> getListByParam(OemCustomerFollowPlan request) {
        /** 1 验证请求参数 */
        //可以设置排序
        /** 2 处理业务 */
        List<OemCustomerFollowPlan> queryList = oemCustomerFollowPlanManager.getListByParam(request);
        /** 3 返回结果 */
        if (CollectionUtils.isNotEmpty(queryList)) {
            return BeanUtil.copyByList(queryList, OemCustomerFollowPlan.class);
        }
        return new ArrayList<>();
    }

    /**
     * 根据条件查询对象list记录
     *
     * @param request
     * @return 分页对象
     */
    @Override
    public PageResponse<OemCustomerFollowPlan> getPageByParam(PageRequest<OemCustomerFollowPlan> request) {
        /** 1 验证请求参数 */
        ValidateUtils.checkObjNull(request, "请求参数不能为空");
        //可以设置排序 query.orderByCreatedTime(true);
        /** 2 处理业务 */
        RowBounds rowBounds = new RowBounds(request.getPageNum(), request.getPageSize());
        Page<OemCustomerFollowPlan> queryPageList = oemCustomerFollowPlanManager.getPageByParam(request.getParamData(), rowBounds);
        PageResponse<OemCustomerFollowPlan> pageResponse = new PageResponse<>();
        pageResponse.converPageResponse(pageResponse, queryPageList, request);
        pageResponse.setDataList(BeanUtil.copyByList(queryPageList.getResult(), OemCustomerFollowPlan.class));
        /** 3 返回结果 */
        return pageResponse;
    }

    /**
     * 根据条件查询单个对象
     *
     * @param request
     * @return
     */
    @Override
    public CustomerFollowPlanResponse getOneByParam(QueryCustomerFollowPlanRequest request) {
        OemCustomerFollowPlan followPlanQuery = BeanUtil.copy(request, OemCustomerFollowPlan.class);
        return DealerCustomerFollowPlanAssembler.transformPOToResponseVo(oemCustomerFollowPlanManager.getOneByParam(followPlanQuery), CustomerFollowPlanResponse.class);
    }

    @Override
    public Integer getFollowUpData(ClueIntentionGradeDataRequest clueIntentionGradeDataRequest) {
        return oemCustomerFollowPlanManager.getFollowUpData(clueIntentionGradeDataRequest);
    }
}
